<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>

<html>
	<head>
		<title>材料库存</title>
	</head>
	<script type="text/javascript">
		$(function(){
			$("#searchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 doSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					doSearch();
				}
			});
			
			$("#table").datagrid({
				title:"材料库存",
				iconCls:'icon-edit',
				loadMsg:'正在加载数据，请稍后......',
				url:'<%=contextPath %>/springMVC/warehouse/listPageMaterialDB',
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				idField:'id',
				frozenColumns:[[
	               // {field:'ck',checkbox:true},
				    {title:'编号',field:'id',hidden:true}
				]],
				columns:[[
					{title:'分类',field:'material_type_name',width:80,align:'center'},
					{title:'材料编号',field:'code',width:80,align:'center'},
					{title:'材料名称',field:'name',width:100,align:'center'},
					{title:'规格型号',field:'format_type',width:100,align:'center'},
					{title:'计量单位',field:'unit_name',width:50,align:'center'},
					{title:'库存总量',field:'num',width:100,align:'center'}
				]],
				view: detailview,
				detailFormatter:function(index,row){
					return '<div style="padding:2px"><table class="ddv"></table></div>';
				},
				onExpandRow: function(index,row){
					var id = row.id;
					
					var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
					 ddv.datagrid({
						title:'分布明细',
						rownumbers:true,
						url:'<%=contextPath %>/springMVC/warehouse/listDBProductNumber?product_id='+id,
						loadMsg:'正在加载数据，请稍后......',
						fitColumns:true,
						columns:[[
							{title:'仓库id',field:'warehouse_id',hidden:true},
							{title:'材料id',field:'product_id',hidden:true},
							{title:'仓库类型',field:'type',hidden:true},
							{title:'部门',field:'dept_name',width:200,align:'center'},
							{title:'仓库名称',field:'warehouse_name',width:300,align:'center'},
							{title:'数量',field:'num',width:200,align:'center'},
							{title:'单价',field:'cost',width:200,align:'center',formatter:function(value,row,index){
								if(row.type == '0' || row.type == '1' || row.type == '4'){
									return "不计";
								}else{
									return value;
								}
							}},
							{title:'总额',field:'total',width:200,align:'center',formatter:function(value,row,index){
								if(row.type == '0' || row.type == '1' || row.type == '4'){
									return "不计";
								}else{
									return Math.round(parseFloat(row.num)*parseFloat(row.cost)*Math.pow(10,2))/Math.pow(10,2);
								}
							}},
							{title:'查看流水',field:'details',width:100,align:'center',formatter: function(value,row,index){
								return "<input type='button'  style='border:0px #ff0000 solid;width:25px;height:15px;cursor:pointer'class='icon-details'  onClick='showLog(\""+row.type+"\",\""+row.warehouse_id+"\",\""+row.product_id+"\")'/>";
							}}
						]],
					 	onResize:function(){
					        $('#table').datagrid('fixDetailRowHeight',index);
					    },
					 	onLoadSuccess:function(){
					       setTimeout(function(){
					         $('#table').datagrid('fixDetailRowHeight',index);
					       },0);
					   }
					 });
					 $('#table').datagrid('fixDetailRowHeight',index);
				},
				pageSize:10,
				pageList : [10,50,100],  
				pagination:true,
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{
						$.messager.alert('提示',rtJson.rtMsrg);
					}
				}
			});
		});
		
		function rtCallBack(){
			$('#table').datagrid('reload');
		}
		
		function showLog(type,warehouse_id,product_id){
			openDialogResize("<%=contextPath %>/springViews/warehouse/dbDetails.jsp?warehouse_id="+warehouse_id+"&product_id="+product_id,1000, 800);
		}
		
		function doSearch()
		{
			var searchValue = $('#searchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$('#table').datagrid('load',{
				searchValue:searchValue,
				material_type_id:material_type_id
				});
		}
		function initDB(type){
			if(type == 0){
				openDialogResize("<%=contextPath %>/springViews/warehouse/beforeDB.jsp?callback=rtCallBack",1000, 800);
			}
			if(type == 1){
				alert("等待项目开发完成");
				return;
				openDialogResize("<%=contextPath %>/springViews/warehouse/projectDB.jsp?callback=rtCallBack",1000, 800);
			}
			if(type == 2){
				openDialogResize("<%=contextPath %>/springViews/warehouse/myDB.jsp?callback=rtCallBack",1000, 800);
			}
			if(type == 3){
				alert("等待订单开发完成");
				return;
				openDialogResize("<%=contextPath %>/springViews/warehouse/orderDB.jsp?callback=rtCallBack",1000, 800);
			}
			if(type == 4){
				openDialogResize("<%=contextPath %>/springViews/warehouse/supplierDB.jsp?callback=rtCallBack",1000, 800);
			}
		}
	</script>
	<body style="height: 97%">
		<div id="table" data-options="fit:true,toolbar:'#tb'" ></div>
		<div id="tb" style="padding:3px;height:auto">
			<div style="float:left;">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="initDB(0);">初始化客户库</a>
<!-- 				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="initDB(1);">初始化甲供库</a> -->
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="initDB(2);">初始化工余料库</a>
<!-- 				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="initDB(3);">初始化订单料库</a> -->
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="initDB(4);">初始化厂家库</a>
			</div>
			<div align="right">
				分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
				查询条件：<input id="searchValue" name="searchValue" style="width:150px" title="材料编码或名称" placeholder="材料编码或名称"/>&nbsp;&nbsp;
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearch();"></a>
			</div>
		</div>
	</body>
</html>
